import json
import pytz
from django.utils import timezone
from django.http import JsonResponse
from django.views import View
from app.models import Salary_theme, User, Salary_college
import glob

class SalaryTheme(View):
    def post(self, req):
        # 在职 入职时间不能是本月 没有转正日期0.8 离职日期
        d = json.loads(req.body)
        print(d)
        users = list(User.objects.filter(department_id=d["department"]).values())
        create_users = []
        tz = pytz.timezone('Asia/Shanghai')
        # 返回时间格式的字符串
        now_time = timezone.now().astimezone(tz=tz)
        current_year = int(now_time.strftime("%Y"))
        current_month = int(now_time.strftime("%m"))
        for item in users:
            print(item)
            if item['regular_time']:
                if item['user_state'] == True:
                    create_users.append({"id":item['id'],"ratio":1})
                    print("在职状态,直接创建")
                else:
                    leave_year = int(item['dimission_time'].split('-')[0])
                    leave_month = int(item['dimission_time'].split('-')[1])
                    print(leave_year,leave_month)
                    if leave_year == current_year:
                        if leave_month == current_month-1:
                            create_users.append({"id":item['id'],"ratio":1})
                            print("离职时间在正常范围,正常创建")
                        elif leave_month == current_month:
                            print("离职时间是本月,不能创建")
                        else:
                            print("离职时间已超过一个月,不能创建")
                    else:
                        print("离职时间不是本年")
            else:
                print(0.8,"实习")
                entry_month = item['entry_time'].split('-')[2]
                if entry_month == current_month:
                    print("刚开始实习，没有工资")
                else:
                    create_users.append({"id":item['id'],"ratio":0.8})
                    print("这是实习状态")
        print(create_users)
        if len(create_users):
            salarys = Salary_theme.objects.create(name=d["name"], attendance=d["attendance"],
                                              salary_start=d["salary_start"], department_id=d["department"],
                                              create_people=d["create_people"], grant_num=len(users))
            for i in create_users:
                print(i)
                Salary_college.objects.create(user_id=i["id"], salary_theme_id=salarys.id, wage_ratio=i["ratio"])
            return JsonResponse({"code": 200, "message": "创建成功"})
        else:
            return JsonResponse({"code": 403, "message": "没有符合条件的人员"})

    def get(self, req):
        page = int(req.GET.get("page"))
        pagesize = int(req.GET.get("pagesize"))
        keyword = req.GET.get("keyword", "")
        start = (page - 1) * pagesize
        end = page * pagesize
        data = list(Salary_theme.objects.filter(name__contains=keyword).values())[start: end]
        c=Salary_theme.objects.all().count()
        return JsonResponse({"code": 200, "message": "获取成功", "data": data,"total":c})

    def put(self, req):
        d = json.loads(req.body)
        ids = d["id"]
        d.pop("id")
        Salary_theme.objects.filter(id=ids).update(**d)
        return JsonResponse({"code": 200, "message": "修改成功"})

    def delete(self, req):
        data=json.loads(req.body)
        Salary_theme.objects.filter(id=data['id']).delete()
        return JsonResponse({"code": 200, "message": "删除成功"})
